#ifndef __TIMER_H
#define __TIMER_H
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "device.h"
#define TIMER_BSP_NAME_MAXLEN 8
extern volatile uint32_t timer_Tick;
#define TIMER_INVALID 0xffffffff

// 申请（就绪）->开始（运行）->到时（就绪）
// 停止（停止）
// 删除（无）
typedef enum
{
	Timer_INVALID,
	Timer_READY,
	Timer_RUN,
	Timer_STOP,

} Timer_State;
typedef enum
{
	Timer_Single,
	Timer_Period,
} Timer_WorkMode;
typedef struct
{
	char name[TIMER_BSP_NAME_MAXLEN];
	Timer_WorkMode WorkMode;
	uint16_t interval;		   // 周期 ms
	uint8_t arrived_time_flag; // 到时标志
	uint32_t start_time;	   // 开始时间
//	uint32_t Next_Time;		   // 下一次到时时间 ms
	uint32_t run_time;		   // 运行时间 ms
//	uint32_t run_time_s;	   // 运行时间 s
	Timer_State Timer_RunState;
	void (*TimerCallBack)(void *param);
	void *param;
	list timer_list;
} Timer;
uint32_t timer_GetTick(void);
void timer_IncTick(void);
int timer_count(void);

Timer_State timer_create(Timer *timer, char *name, uint32_t interval,
						 Timer_WorkMode WorkMode,
						 void (*TimerCallBack)(void *param),
						 void *param);
Timer_State timer_state(Timer *timer);
Timer *timer_find(char *name);
void timer_start(Timer *timer);
void timer_stop(Timer *timer);

void timer_delete(Timer *timer);
void timer_modify(Timer *timer, Timer_WorkMode WorkMode, uint32_t interval);
void timer_IrqHandle(void);
void delay_us(uint32_t us);
void delay_ms(uint16_t ms);
void timer_Polling(void);
#endif
